/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.renderer;

import java.io.IOException;

import org.skatiger.jrove.ResponseWriter;
import org.skatiger.jrove.component.JroveAttr;
import org.skatiger.jrove.component.UIComponent;

public class RendererUtils {

    public static boolean renderHtmlAttribute(ResponseWriter writer,
            UIComponent component, String htmlAttrName, String propertyName)
            throws IOException {
        String propName = htmlAttrName.equals(Html.CLASS_ATTR) ? JroveAttr.STYLE_CLASS_ATTR
                : htmlAttrName;//if renderer html attribute class value, then get styleClass from component
        Object attrValue = component.getAttributes().get(propName);
        return renderHtmlAttributeValue(writer, htmlAttrName, propName,
                attrValue);
    }

    public static boolean renderHtmlStandardAttributes(ResponseWriter writer,
            UIComponent component) throws IOException {
        return renderHtmlStandardAttributes(writer, component, null);
    }

    public static boolean renderHtmlStandardAttributes(ResponseWriter writer,
            UIComponent component, String[] optionalAttributes)
            throws IOException {
        boolean doSomething = false;
        if (renderHtmlAttributes(writer, component,
                Html.STANDARD_ATTRIBUTES_WITHOUT_ID)) {
            doSomething = true;
        }

        if (renderHtmlAttributes(writer, component, optionalAttributes)) {
            doSomething = true;
        }
        if (renderHtmlAttributes(writer, component, Html.STANDARD_EVENTS)) {
            doSomething = true;
        }

        return doSomething;
    }

    public static boolean renderHtmlAttributes(ResponseWriter writer,
            UIComponent component, String[] attributes) throws IOException {
        boolean doSomething = false;
        if (attributes != null && attributes.length > 0) {
            for (String attrName : attributes) {
                if (renderHtmlAttribute(writer, component, attrName, attrName)) {
                    doSomething = true;
                }
            }
        }
        return doSomething;
    }

    public static boolean renderHtmlAttribute(ResponseWriter writer,
            UIComponent component, String propertyName) throws IOException {
        return renderHtmlAttribute(writer, component, propertyName,
                propertyName);
    }

    public static boolean renderHtmlAttributeValue(ResponseWriter writer,
            String htmlAttrName, String propertyName, Object value)
            throws IOException {

        if (value != null) {
            String strValue = value.toString();
            if (!strValue.trim().isEmpty()) {
                writer.writeAttribute(htmlAttrName, value, propertyName);
                return true;
            }
        }
        return false;
    }

}
